/**
 * 
 */
package com.wisdytech.linkdcs.equipment.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.session.UserSession;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipment.model.Client;
import com.wisdytech.linkdcs.equipment.service.ICollectTerminalService;
import com.wisdytech.linkdcs.equipment.service.IEquipmentConnectService;
import com.wisdytech.linkdcs.equipment.service.IEquipmentRegisteredService;
import com.wisdytech.linkdcs.equipment.vo.CollectTerminalVO;
import com.wisdytech.linkdcs.equipment.vo.EquipmentConnectVO;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * <p>
 * ClassName: CollectTerminalAction
 * </p>
 * <p>
 * Description: 采集终端管理页面的action层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-29
 * </p>
 */
@Controller
@RequestMapping("/equipment/terminal")
public class CollectTerminalAction {
    @Autowired
    @Qualifier("collectTerminalService")
    private ICollectTerminalService collectTerminalService;
    @Autowired
    @Qualifier("equipmentRegisterService")
    private IEquipmentRegisteredService equipmentRegisterService;
    
    @Resource
    private IEquipmentConnectService equipmentConnectService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, String clientCode, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        
        if(StringUtils.isNotBlank(clientCode)){
            model.addAttribute("clientCode",clientCode);
        }
        return "equipment/collectTerminal/collectTerminal";
    }
    
    
    /**
     * 分页条件查询设备终端管理页面信息列表
     * @param clientCode 终端编码
     * @param filter 分页条件
     * @return 设备终端管理页面信息列表
     */
    @RequestMapping("/listCollectTerminalByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<CollectTerminalVO> listCollectTerminalByFilter(String clientCode,String operationId, String clientName, Filter filter) {
       PageInfo<Client> collectTerminals = collectTerminalService.listCollectTerminalByFilter(clientCode, clientName, operationId, filter);
       //转换为前台结果集
       List<Client> collectList = collectTerminals.getList();
       List<CollectTerminalVO> collectTerminalVOList=new ArrayList<CollectTerminalVO>();
       for(Client client: collectList){
           collectTerminalVOList.add(DataTransformUtil.changeCollectTerminalVO(client));
       }
       return new StandardTableResult<CollectTerminalVO>(collectTerminals.getTotal(),collectTerminalVOList);
    }
    /**
     * 保存采集终端对象
     * @param client 采集终端对象
     * @return 采集终端对象 
     */
    @RequestMapping("/saveCollectTerminal/save.mvc")
    @ResponseBody
    public StandardResult saveCollectTerminal(Client client, HttpSession session){
        SysUser userSession = BaseUtil.getCurrentUser();
        String loginName = userSession.getUsername();
        if (StringUtils.isBlank(client.getPkId())) {
        	
          return  collectTerminalService.addCollectTerminal(client, loginName);
        }else{
           return collectTerminalService.updateCollectTerminal(client, loginName);
        }
    }
    /**
     * 通过主键id查询对应的采集终端信息
     * @param pkId 主键id
     * @return 采集终端信息对象
     */
    @RequestMapping("/getCollectTerminalById/get.mvc")
    @ResponseBody
    public CollectTerminalVO getCollectTerminalById(String pkId){
        Client client = new Client();
        client = collectTerminalService.getCollectTerminalById(pkId);

        /*//判断设备中是否关联采集终端--如果关联，给equipClientId任意赋值---这里默认赋值为1
        List<Equipment> equipmentList = equipmentRegisterService.selectByCilentId(client.getPkId());
        String equipClientId = "";
        if (!equipmentList.isEmpty()) {
            equipClientId = "1";
        }*/
        
        int num =equipmentConnectService.countEquipmentCollectByClient(client.getPkId());
        
        
        CollectTerminalVO terminalVO = new CollectTerminalVO();
        terminalVO = DataTransformUtil.changeCollectTerminalVO(client, num);
        return terminalVO;
    }
    /**
     * 通过主键id删除对应的采集终端信息
     * @param pkId 主键id
     * @return 采集终端信息对象 
     */
    @RequestMapping("/delCollectTerminalById/del.mvc")
    @ResponseBody
    public StandardResult delCollectTerminalById(String pkId) {
        return collectTerminalService.delCollectTerminalById(pkId);
    }
    
    /**
     * <p>Description:查询采集终端所有信息列表</p> 
     * @return 采集终端信息列表集合
     */
    @RequestMapping(value = "/listAllCollectTerminal/get.mvc")
    @ResponseBody
    public List<Client> listAllCollectTerminal(String pkId){
        return collectTerminalService.listAllCollectTerminal();
    }
    
    /**
     * 通过终端编号查询对应的终端信息
     * @param clientCode 终端编号
     * @return 采集终端信息对象
     */
    @RequestMapping(value = "/getCollectTerminalByClientCode/get.mvc")
    @ResponseBody
    public EquipmentConnectVO getCollectTerminalByClientCode(String clientCode){
        Client client = collectTerminalService.getCollectTerminalByClientCode(clientCode);
        EquipmentConnectVO connectVO = new EquipmentConnectVO();
        if (client !=null ) {
            connectVO  = DataTransformUtil.changeEquipmentConnectVO(client);
        }
        return connectVO;
    }
    
    
    
}
